package com.dwxt.cdms.applets;

import com.dwxt.cdms.applets.service.ConversationService;
import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.dao.CustomerMapper;
import com.dwxt.cdms.entity.Customer;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 安博熙小程序
 */
@RequestMapping("/cdms/conversation")
@RestController
public class ConversationController {

    @Autowired
    private ConversationService conversationService;
    @Autowired
    private CustomerMapper customerMapper;
    /**
     * 添加我可以查看的关联人，生成消息
     * @param customerId
     * @param relateId
     * @return
     */
    /*@RequestMapping("/addRelation")
    public BaseResult addRelation(Integer customerId,Integer relateId){
        return conversationService.addRelation(customerId,relateId);
    }*/
    @RequestMapping("/addRelation")
    public BaseResult addRelation(Integer customerId,String phone,String idNumber ,String relate,Boolean type){

        // 根据电话和身份证查询用户 ， 如果存在则保存信息关系
        Customer customer = customerMapper.selectByPhone(phone);
        if (customer!=null&&idNumber.equals(customer.getIdNumber())){
            if (type!=null && type) {
                return conversationService.addRelation(customer.getId(),customerId,relate);
            }else {
                return conversationService.addRelation(customerId,customer.getId(),relate);
            }
        }else {
            return BaseResult.build(StatusCode.USER_NOT_EXIST);
        }


//        return conversationService.addRelation(customerId,relateId);
    }

    /**
     * 查询消息分页
     * @param page
     * @param customerId
     * @return
     */
    @RequestMapping("/selectConversionPage")
    public BaseResult selectConversionPage(Page page,Integer customerId,Integer type){
        return conversationService.selectConversionPage(page,customerId,type);
    }

    /**
     * 同意或拒绝添加
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/agreeOrRefund")
    public BaseResult agreeOrRefund(Integer id,Integer status){
        return conversationService.agreeOrRefund(id,status);
    }


    /**
     * 设置为old的消息不能再次回复
     * @param customerId
     * @return
     */
    @RequestMapping("/updateConversationToOld")
    public BaseResult updateConversationToOld(Integer customerId){
        return conversationService.updateConversationToOld(customerId);
    }

    /**
     * 是否有新消息
     * @param customerId
     * @return
     */
    @RequestMapping("/isNewConversation")
    public BaseResult isNewConversation(Integer customerId){

        return conversationService.isNewConversation(customerId);
    }

}
